﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using csvm2.message.contract;
using csvm2.pin.contract;

namespace csvm2.pagebuilder
{
    public class PageBuilder : IPageBuilder
    {
        private const int PAGE_SIZE = 10;

        private List<Record> currentPageRecords = new List<Record>();


        public void ProcessRecord(Record rec)
        {
            if (rec == null)
                EmitPage();
            else
            {
                if (this.currentPageRecords.Count == PAGE_SIZE) EmitPage();
                this.currentPageRecords.Add(rec);
            }
        }


        private void EmitPage()
        {
            this.OnPage(new Page(this.currentPageRecords));
            this.currentPageRecords = new List<Record>();
        }


        public event Action<Page> OnPage;
    }
}
